<template>
    <div style="text-align: center;margin: 0 20px">
        <div style="margin-top: 150px">
            <div style="font-size: 25px;font-weight: bold">登录</div>
            <div style="font-size: 14px;color: grey">在进入系统之前请先输入用户名和密码进行登录</div>
        </div>
        <div style="margin-top: 50px">
            <el-form :model="form" :rules="rules" ref="formRef">
                <el-form-item prop="username">
                    <el-input v-model="form.username" maxlength="20" type="text" placeholder="用户名/邮箱">
                        <template #prefix>
                            <el-icon>
                                <User/>
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="form.password" type="password" maxlength="20" style="margin-top: 10px" placeholder="密码">
                        <template #prefix>
                            <el-icon>
                                <Lock/>
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-row style="margin-top: 5px">
                    <el-col :span="12" style="text-align: left">
                        <el-form-item prop="remember">
                            <el-checkbox v-model="form.remember" label="记住我"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" style="text-align: right">
                        <el-link @click="router.push('/forget')">忘记密码？</el-link>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div style="margin-top: 40px">
            <el-button @click="LoginBefore()" style="width: 270px" type="success" plain>立即登录</el-button>
        </div>
        <el-divider>
            <span style="color: grey;font-size: 13px">没有账号</span>
        </el-divider>
        <div>
            <el-button style="width: 270px" @click="router.push('/register')" type="warning" plain>注册账号</el-button>
        </div>
        <div>
            <Vcode :show="isShow" @success="success" @close="close" @fail="fail"></Vcode>
        </div>
    </div>

</template>

<script setup>
    //引入'vue3-puzzle-vcode'插件
    import Vcode from 'vue3-puzzle-vcode'
    import {User, Lock} from '@element-plus/icons-vue'
    import router from "@/router";
    import {reactive, ref} from "vue";
    import {login} from '@/net'
    import { onMounted } from 'vue'

    onMounted(() => {
        //绑定监听事件
        window.addEventListener('keydown', keyDown)
    });
    const keyDown = (e) => {
        if (e.keyCode === 13 || e.keyCode === 100) {
            formRef.value.validate((isValid) => {
                if (isValid) {
                    isShow.value = true
                }
            });
        }
    }

    const flag=ref(false);
    //验证码模态框是否出现，默认不展示
    const isShow = ref(false)
    //用户点击遮罩层，关闭模态框
    const close = () => {
        isShow.value = false
    }
    //用户通过了验证
    const success = () => {
        isShow.value = false
        flag.value=true;
        console.log('验证通过')
        userLogin()
    }

    //用户验证失败
    const fail = () => {
        console.log('验证失败')
    }


    const formRef = ref()
    const form = reactive({
        username: '',
        password: '',
        remember: false
    })

    const rules = {
        username: [
            { required: true, message: '请输入用户名' }
        ],
        password: [
            { required: true, message: '请输入密码'}
        ]
    }
    function LoginBefore(){
        formRef.value.validate((isValid) => {
            if (isValid) {
                isShow.value = true
            }
        });
    }
    function userLogin() {
        login(form.username, form.password, form.remember, (data) => {
            console.log(data.role)
            sessionStorage.setItem('role',data.role);
            if(data.role === '管理员'){
                router.push("/super")
            }else if(data.role==='操作员'){
                router.push('/home')
            }else if(data.role==='质检员'){
                router.push('/home')
            }else if(data.role==='仓库管理员'){
                router.push('/home')
            }else if(data.role==='销售员'){
                router.push('/home')
            }else if(data.role==='客户'){
                router.push('/customer')
            }
        })
    }
</script>

<style scoped>

</style>
